package com.fiestas.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper{

	
	private final String CREATE_TABLE_SOCIO_QUERY = "CREATE TABLE users ( name TEXT PRIMARY KEY COLLATE NOCASE , surname TEXT, pathImage TEXT,password INTEGER NOT NULL)";
	private final String CREATE_TABLE_PAGOS_QUERY = "CREATE TABLE payments ( ref INTEGER PRIMARY KEY AUTOINCREMENT, nameSocio TEXT, name TEXT NOT NULL, cuantity DOUBLE NOT NULL, FOREIGN KEY(nameSocio) REFERENCES users(name) ON DELETE CASCADE ON UPDATE CASCADE)";
	private final String CREATE_TABLE_EVENTOS_QUERY = "CREATE TABLE events ( _id INTEGER PRIMARY KEY AUTOINCREMENT, nameSocio TEXT, name TEXT NOT NULL, date TEXT NOT NULL, FOREIGN KEY(nameSocio) REFERENCES users(name) ON DELETE CASCADE ON UPDATE CASCADE)";
	
	
	public DBHelper(Context context, String name, CursorFactory factory,int version) {
		super(context, name, factory, version);
	}

	
	

	@Override
	public void onCreate(SQLiteDatabase db) {
		
		/**
		 * Check if databases exists.
		 */
		db.execSQL("DROP TABLE IF EXISTS users");
		db.execSQL("DROP TABLE IF EXISTS payments");
		db.execSQL("DROP TABLE IF EXISTS events");
		
		db.execSQL(CREATE_TABLE_SOCIO_QUERY);
		db.execSQL(CREATE_TABLE_PAGOS_QUERY);
		db.execSQL(CREATE_TABLE_EVENTOS_QUERY);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		/**
		 * Check if databases exists.
		 */
		db.execSQL("DROP TABLE IF EXISTS users");
		db.execSQL("DROP TABLE IF EXISTS payments");
		db.execSQL("DROP TABLE IF EXISTS events");
		
		db.execSQL(CREATE_TABLE_SOCIO_QUERY);
		db.execSQL(CREATE_TABLE_PAGOS_QUERY);
		db.execSQL(CREATE_TABLE_EVENTOS_QUERY);
		
	}
	public void updatePragmaRule(SQLiteDatabase db){
		db.execSQL("PRAGMA foreign_keys=ON;");
		
	}
}
